﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace employee.Controls
{
    public partial class emWorkInfo : common.controls.baseUserControl
    {
        public emWorkInfo()
        {
            InitializeComponent();
            managerNameEd.BackColor = common.Settings.myTheme.Info.BackColor;
        }
        BindingSource lastDataSrc = null;
        public virtual void Init(BindingSource dataSrc)
        {
            companyCb.LoadData();
            departmentCb.LoadData();
            workGroupCb.LoadData();
            emTypeCb.LoadData();
            recruitSourceCb.LoadData();
            wageTypeCb.LoadData();
            SetDataSource(dataSrc);
            this.lastDataSrc = dataSrc;
        }
        public new void Refresh()
        {
            if (this.lastDataSrc != null) SetDataSource(this.lastDataSrc);
            ShowManagerInfo(managerCodeEd.Text);
        }

        protected void SetDataSource(BindingSource dataSrc)
        {
            common.BindingLibs.BindObject(emTypeCb, dataSrc, DataAccess.DataCache.myBaseDS.employee.emTypeColumn.ColumnName);
            
            common.BindingLibs.BindObject(joinDateEd, dataSrc, DataAccess.DataCache.myBaseDS.employee.joinDateColumn.ColumnName);
            common.BindingLibs.BindObject(quitDateEd, dataSrc, DataAccess.DataCache.myBaseDS.employee.quitDateColumn.ColumnName);

            common.BindingLibs.BindObject(recruitSourceCb, dataSrc, DataAccess.DataCache.myBaseDS.employee.recruitSourceColumn.ColumnName,"myValue");

            common.BindingLibs.BindObject(recruitNoteEd, dataSrc, DataAccess.DataCache.myBaseDS.employee.recruitNotesColumn.ColumnName);

            common.BindingLibs.BindObject(companyCb, dataSrc, DataAccess.DataCache.myBaseDS.employee.workCompanyColumn.ColumnName);
            common.BindingLibs.BindObject(departmentCb, dataSrc, DataAccess.DataCache.myBaseDS.employee.workDeptColumn.ColumnName);
            common.BindingLibs.BindObject(workGroupCb, dataSrc, DataAccess.DataCache.myBaseDS.employee.workGroupColumn.ColumnName);

            common.BindingLibs.BindObject(wageTypeCb, dataSrc, DataAccess.DataCache.myBaseDS.employee.wageTypeColumn.ColumnName);

            common.BindingLibs.BindObject(managerCodeEd, dataSrc, DataAccess.DataCache.myBaseDS.employee.managerCodeColumn.ColumnName);
            
            common.BindingLibs.BindObject(workPositionEd, dataSrc, DataAccess.DataCache.myBaseDS.employee.workPositionColumn.ColumnName);
            common.BindingLibs.BindObject(workDescEd , dataSrc, DataAccess.DataCache.myBaseDS.employee.workDescriptionColumn.ColumnName);

            recruitNoteEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.recruitNotesColumn.MaxLength;
            workPositionEd.MaxLength = DataAccess.DataCache.myBaseDS.employee.workPositionColumn.MaxLength;
        }

        public virtual bool CheckData() 
        {
            bool retVal = true;
            ClearNotifyError();
            if (companyCb.myValue.Trim() == "")
            {
                NotifyError(companyLbl);
                retVal = false;
            }
            if (departmentCb.myValue.Trim() == "")
            {
                NotifyError(emDepartmentLbl);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            state = !state;
            emTypeCb.Enabled = state;
            joinDateEd.Enabled = state;
            quitDateEd.Enabled = state;
            
            recruitSourceCb.Enabled = state;
            recruitNoteEd.Enabled = state;

            companyCb.Enabled = state;
            departmentCb.Enabled = state;
            workGroupCb.Enabled = state;

            managerCodeEd.Enabled = state;

            wageTypeCb.Enabled = state;

            workDescEd.Enabled = state;
            workPositionEd.Enabled = state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            emTypeCb.Focus();
        }

        private void ShowManagerInfo(string code)
        {
            if (code == null)
            {
                managerCodeEd.Text = ""; managerNameEd.Text = "";
                return;
            }
            data.baseDS.employeeRow employeeRow = DataAccess.DataLibs.GetEmloyee(code);
            if (employeeRow != null)
            {
                managerCodeEd.Text = employeeRow.emCode;
                managerNameEd.Text = employeeRow.fullName.Trim();
            }
        }
        private void managerCodeEd_Validated(object sender, EventArgs e)
        {
            try
            {
                ShowManagerInfo(managerCodeEd.Text);
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
    }
}
